package com.ruoyi.web.controller.weiriya;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.WMember;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.core.domain.entity.WVehicle;
import com.ruoyi.weiriya.service.IWMemberService;
import com.ruoyi.weiriya.service.IWVehicleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WVipSetting;
import com.ruoyi.weiriya.service.IWVipSettingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员购买设置Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "会员购买设置管理")
@RestController
@RequestMapping("/weiriya/vipSetting")
public class WVipSettingController extends BaseController
{
    @Autowired
    private IWVipSettingService wVipSettingService;

    @Autowired
    private IWMemberService memberService;
    @Autowired
    private IWVehicleService vehicleService;

    /**
     * 查询会员购买设置列表
     */
    @ApiOperation("获取会员购买设置列表")
    @GetMapping("/list")
    public TableDataInfo list(WVipSetting wVipSetting)
    {
        startPage();
        List<WVipSetting> list = wVipSettingService.getList(wVipSetting);
        return getDataTable(list);
    }


    /**
     * 导出会员购买设置列表
     */
    @ApiOperation("导出会员购买设置列表")
    @Log(title = "会员购买设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WVipSetting wVipSetting)
    {
        List<WVipSetting> list = wVipSettingService.getList(wVipSetting);
        ExcelUtil<WVipSetting> util = new ExcelUtil<WVipSetting>(WVipSetting.class);
        util.exportExcel(response, list, "会员购买设置数据");
    }

    /**
     * 导入会员购买设置列表
     */
    @ApiOperation("导入会员购买设置列表")
    @Log(title = "会员购买设置", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WVipSetting> util = new ExcelUtil<WVipSetting>(WVipSetting.class);
        List<WVipSetting> list = util.importExcel(file.getInputStream());
        return success(wVipSettingService.saveBatch(list));
    }

    /**
     * 获取会员购买设置详细信息
     */
    @ApiOperation("获取会员购买设置详细")
    @ApiImplicitParam(name = "id", value = "会员购买设置ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = {"/{id}", "/{id}/{settingType}"})
    public AjaxResult getInfo(@PathVariable("id") String id, @PathVariable(value = "userId", required = false) Integer settingType)
    {
        return success(wVipSettingService.getWVipSettingById(id, settingType));
    }

    /**
     * 用户获取点位办卡收费规则
     */
    @ApiOperation("用户获取点位办卡收费规则")
    @GetMapping(value = {"/getByMember"})
    public AjaxResult getByMember()
    {
        WMember member = memberService.getById(SecurityUtils.getUserId());
        WVehicle vehicle = vehicleService.getOne(new LambdaQueryWrapper<WVehicle>()
                .eq(WVehicle::getMemberId, member.getId()));
        if(member.getMemberType() == 1){
             return success(wVipSettingService.getOne(new LambdaQueryWrapper<WVipSetting>()
                    .eq(WVipSetting::getAreaId, member.getAreaId())
                    .eq(WVipSetting::getSettingType, 1)
                    .eq(WVipSetting::getVehicleType, vehicle.getVehicleChargeTypeId())));
        }
        if(member.getMemberType() == 2){
             return success(wVipSettingService.getOne(new LambdaQueryWrapper<WVipSetting>()
                    .eq(WVipSetting::getAreaId, member.getAreaId())
                    .eq(WVipSetting::getSettingType, 2)
                    .eq(WVipSetting::getVehicleType, vehicle.getVehicleNonChargeTypeId())));
        }
        return success();
    }

    /**
     * 新增会员购买设置
     */
    @ApiOperation("新增会员购买设置")
    @Log(title = "会员购买设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WVipSetting wVipSetting)
    {
        return toAjax(wVipSettingService.saveWVipSetting(wVipSetting));
    }

    /**
     * 修改会员购买设置
     */
    @ApiOperation("更新会员购买设置")
    @Log(title = "会员购买设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<WVipSetting> list)
    {
        for (WVipSetting wVipSetting : list) {
            wVipSettingService.updateWVipSettingById(wVipSetting);
        }
        return toAjax(true);
    }

    /**
     * 删除会员购买设置
     */
    @ApiOperation("删除会员购买设置")
    @ApiImplicitParam(name = "ids", value = "会员购买设置ID集合", required = true, dataType = "String", paramType = "path")
    @Log(title = "会员购买设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wVipSettingService.removeByIds(ids));
    }
}
